//
//  ViewController.m
//  05_变换
//
//  Created by apple on 16/5/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *layerView1;

@property (weak, nonatomic) IBOutlet UIView *layerView2;

@property (weak, nonatomic) IBOutlet UIView *layerView3;

@property (weak, nonatomic) IBOutlet UIView *layerView4;

@property (weak, nonatomic) IBOutlet UIView *layerView5;

@property (weak, nonatomic) IBOutlet UIImageView *imageView1;

@property (weak, nonatomic) IBOutlet UIImageView *imageView2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self atrributeOfCGAffineTransform];
    
    [self TiltChange];
    
    [self atrributeOfCATransform3D];
    
    [self atrributeOfSublayerTransform];
    

}


/** 仿射变换 (CGAffineTransform)*/
- (void)atrributeOfCGAffineTransform{

    //CGAffineTransform : 用于在二维空间做旋转, 缩放 和平移( 3*3 矩阵)
    //特点 : 平行的两条线 在变换之后 仍保持平行.
    //CGAffineTransformMakeRotation : 旋转 (弧度计算 , 正数为顺时针)
    //CGAffineTransformMakeScale : 缩放
    //CGAffineTransformMakeTranslation : 平移
    
    UIImage *image = [UIImage imageNamed:@"pic_k2.jpg"];
    self.layerView1.layer.contents = (id)image.CGImage;
    
    self.layerView1.layer.affineTransform = CGAffineTransformMakeRotation(M_PI / 4.0);
    
    
    //混合变换
    //可以用CGAffineTransformConcat(CGAffineTransform t1, CGAffineTransform t2);
    //生成一个新的矩阵 , 实现混合变换
    //也可以在第一次变换的基础上 , 继续变换 .
    self.layerView2.layer.contents = (id)image.CGImage;
    
    //创建一个空的 transform
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    transform = CGAffineTransformScale(transform, 0.5, 0.5);
    transform = CGAffineTransformTranslate(transform, 100, 0);
    transform = CGAffineTransformRotate(transform, M_PI/6.0);
    
//    transform = CGAffineTransformConcat(CGAffineTransformMakeRotation(M_PI / 6.0), CGAffineTransformMakeTranslation(100, 0));
    
    self.layerView2.layer.affineTransform = transform;
    

}


/**剪切变换 (斜切变换)*/

- (void)TiltChange{

    //a*X + c*Y + tx = X'
    //b*X + d*Y + ty = Y'
    // transform 默认值为  a=1 , b=0 , c=0 , d=1 , tx=0 , ty=0
    // tx 与 ty 控制视图的偏移量
    // a 与 d 控制视图的放大缩小.
    // c 控制左右倾斜 , b 控制上下倾斜 (正数为 , 右 , 下)
    UIImage *image = [UIImage imageNamed:@"pic_k3.jpg"];
    
    self.layerView3.layer.contents = (id)image.CGImage;
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform.c = -0.5;
    transform.b = 0;
    
    self.layerView3.layer.affineTransform = transform;
    
}


/** 3D变换 (CATransform3D) */

- (void)atrributeOfCATransform3D{

    //CATransform3D : 让图层在3D空间内移动或旋转 (4*4矩阵)
    //CATransform3DMakeRotation(CGFloat angle, CGFloat x, CGFloat y, CGFloat z)
    //CATransform3DMakeScale(CGFloat sx, CGFloat sy, CGFloat sz)
    //CATransform3DMakeTranslation(Gloat tx, CGFloat ty, CGFloat tz)
    //X轴右正 , Y轴下正 , Z轴屏幕垂直向外为正.
    //旋转: 顺时针旋转为正.
    
    UIImage *image = [UIImage imageNamed:@"pic_k3.jpg"];
    
    self.layerView4.layer.contents = (id)image.CGImage;
    CATransform3D transform = CATransform3DIdentity;
    
    
    
    //3D视角中 , 近视角的边要长 , 远视角的边要长.
    //需引入投影变换(Z变换.)
    //矩阵中 m34 元素 来控制要离视角多远.
    //通过设置 m34 -1.0/d 来应用透视效果(d 一般设置为500~1000)
    //-> d代表想象中视角相机与屏幕之间的距离.
    //注意!: 先投影变换 , 再旋转!!!!!!!!!
    
    transform.m34 = -1.0 / 500.0;
    
    transform = CATransform3DRotate(transform,M_PI/4.0, 1 , 0, 0);
    
    self.layerView4.layer.transform = transform;
    
    
    //灭点 (指的是立体图形各条边的延伸线所产生的相交点。)
    //CA 定义图层的灭点为 anchorPoint点.
    

}


/** 3D变换2(sublayerTransform 属性) */

- (void)atrributeOfSublayerTransform{

    //sublayerTransform : 也是CATransform3D类型
    //对所有子视图 都有影响.
    //可以保证子视图有 相同的透视和灭点.
    //加约束时 , 要谨慎 . (不能加 上下边对齐约束)
    
    CATransform3D transform = CATransform3DIdentity;
    
    transform.m34 = -1.0 / 500.0;
    
    self.layerView5.layer.sublayerTransform = transform;
    
    self.imageView1.layer.transform = CATransform3DMakeRotation(M_PI/4.0, 0, 1, 0);

    self.imageView2.layer.transform = CATransform3DMakeRotation(M_PI/4.0, 0, -1, 0);

}


/** 3D变换后续内容 , 详情查看 VCOfSecond(第二页面)*/










@end
